<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        li {
            list-style: none;
            transition: 0.5s;
        }

        .box {
            width: 700px;
            margin: 0 auto;
            position: relative;

        }

        .box ul {
            width: 700px;
            height: 400px;
            position: relative;

        }

        .box ul li {
            width: 100%;
            height: 100%;
            position: absolute;
            left: 0;
            top: 0;
            opacity: 0;
            text-align: center;
            line-height: 400px;
            font-size: 50px;
            border: 1px solid #000;
        }

        .box ul li img {
            width: 100%;
            height: 100%;

        }

        .box ul .on {
            opacity: 1;
        }

        .jianTo {
            width: 100%;
            height: 50px;
            display: flex;
            justify-content: space-between;
            position: absolute;
            left: 0;
            top: 50%;
            margin-top: -25px;
        }

        .jianTo span {
            width: 50px;
            height: 50px;
            font-size: 30px;
            line-height: 50px;
            background: rgba(0, 0, 0, 0.7);
            text-align: center;
            color: #f2f3ef;
            cursor: pointer;
        }

        ol {
            position: absolute;
            bottom: 0;
            display: flex;
            justify-content: center;
            width: 100%;
        }

        ol li {
            width: 30px;
            height: 30px;
            background-color: #f00;
            margin: 10px;
        }

        .active {
            background-color: green;
        }
    </style>
</head>

<body>

    <div class="box" id="a">
        <ul>
            <li class="on">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>
        <ol>
            <li class="active"></li>
            <li></li>
            <li></li>
            <li></li>
        </ol>
        <div class="jianTo">
            <span class="prev">&lt;</span>
            <span class="next">&gt;</span>
        </div>
    </div>


    <div class="box" id="b">
        <ul>
            <li class="on">1</li>
            <li>2</li>
            <li>3</li>
        </ul>
        <ol>
            <li class="active"></li>
            <li></li>
            <li></li>
        </ol>
        <div class="jianTo">
            <span class="prev">&lt;</span>
            <span class="next">&gt;</span>
        </div>
    </div>


    <script>


        // 对象是由属性和方法构成的
        //    构造函数中，尽量写属性和方法   --- 逻辑都封装成对应的函数


        function Swiper(el, options) {
            const { dotFlag, arrowFlag, time } = options;   // 解构
            this.time = time;
            this.dotFlag = dotFlag;
            this.arrowFlag = arrowFlag;


            // 获取相关元素
            this.oBox = document.querySelector(el);
            this.oImgs = this.oBox.querySelectorAll('ul li');
            this.oDotBox = this.oBox.querySelector('ol');
            this.oDots = this.oBox.querySelectorAll('ol li');
            this.oPrev = this.oBox.querySelector('.prev');
            this.oNext = this.oBox.querySelector('.next');

            // index 控制播放第几张
            this.index = 0;
            // timer 控制定时器
            this.timer = null;


            this.init();

        }

        // 自动播放功能
        Swiper.prototype.auoPlay = function () {
            this.timer = setInterval(() => {    // 维持这个this指向轮播图
                this.index++;
                if (this.index === this.oImgs.length) {
                    this.index = 0;
                }
                this.show();
            }, this.time);
        }

        // 显示index对应的这张图片
        Swiper.prototype.show = function () {
            // 清除所有的
            for (let i = 0; i < this.oImgs.length; i++) {
                this.oImgs[i].classList.remove('on');
                this.oDots[i].classList.remove('active');
            }
            // index对应的这一张显示
            this.oImgs[this.index].classList.add('on');
            this.oDots[this.index].classList.add('active');
        }

        // 上下翻页
        Swiper.prototype.arrowEvent = function () {
            // 上翻页
            this.oPrev.onclick = () => {
                clearInterval(this.timer);
                this.index--;
                if (this.index === -1) {
                    this.index = this.oImgs.length - 1;
                }
                this.show();
            }
            // 下翻页
            this.oNext.onclick = () => {
                clearInterval(this.timer);
                this.index++;
                if (this.index === this.oImgs.length) {
                    this.index = 0;
                }
                this.show();
            }
        }

        // 小点切换
        Swiper.prototype.dotEvent = function () {
            // const _this = this;
            for (let i = 0; i < this.oDots.length; i++) {
                this.oDots[i].onclick = () => {
                    clearInterval(this.timer);
                    this.index = i;
                    this.show()
                }
            }
        }

        // 鼠标移入移出
        Swiper.prototype.overEvent = function () {
            this.oBox.onmouseover = () => {
                clearInterval(this.timer)
            }
            this.oBox.onmouseout = () => {
                this.auoPlay()
            }
        }


        // 初始化
        Swiper.prototype.init = function () {
            this.auoPlay()


            // 判断有没有小点
            if (this.dotFlag) {
                this.dotEvent()
            }
            else {
                this.oDotBox.style.display = 'none'
            }

            // 判断有没有左右箭头
            if (this.arrowFlag) {
                this.arrowEvent()
            }
            else {
                this.oPrev.style.display = 'none'
                this.oNext.style.display = 'none'
            }


            this.overEvent()
        }




        const s = new Swiper('#a', {
            time: 2000,
            dotFlag: true,
            arrowFlag: false
        });


        const s2 = new Swiper('#b', {
            time: 1000,
            dotFlag: false,
            arrowFlag: true
        });




    </script>

</body>

</html>